// 1.引入路由方法
import {createRouter,createWebHashHistory} from 'vue-router'

// 2.引入页面组件
import Index from '../layout/index.vue'
import Login from '../views/Login.vue'
import task from '../views/TaskCenter/index.vue'
import showc from '../views/ShowCenter/index.vue'
import control from '../views/ControlCenter/index.vue'
import system from '../views/SystemCenter/index.vue'
import {
  LogOutOutline as HomeIcon,
  LaptopOutline as WorkIcon
} from "@vicons/ionicons5";
import { NIcon } from 'naive-ui';
import { h } from "vue";
import Flycontrol from '../views/ShowCenter/flycontrol.vue'
import Taskshow from '../views/ShowCenter/taskshow.vue'
import Flysquare from '../views/ControlCenter/flysquare.vue'
import Taskcontrol from '../views/TaskCenter/taskcontrol.vue'
import Tasklog from '../views/TaskCenter/tasklog.vue'
import Person from '../views/SystemCenter/person.vue'
import { useAccount } from '../store/account'
import { checkToken } from '../api/user'
import Role from '../views/SystemCenter/Role.vue'
import Nopermission from '../views/Nopermission.vue'
import User from '../views/Setting/user.vue'
import Taskline from '../views/TaskCenter/taskline.vue'
import Taskstop from '../views/TaskCenter/taskstop.vue'

function renderIcon(icon) {
  return () => h(NIcon, null, { default: () => h(icon) });
}
// 3.设置路由-组件的映射关系
export const routes = [
  {
    path: '/',
    component: Index,
    show: true,      // 新增菜单可见性控制
    children: [
      {
        path: '/fly',
        component: showc,
        label: '管控中心',  // 新增菜单项名称
        children:[
          {
            path: '/fly/flycontrol',
            component: Flycontrol,
            label: '飞行控制台',  // 新增菜单项名称
          },
          {
            path: '/fly/taskshow',
            component: Taskshow,
            label: '作业现场',  // 新增菜单项名称
          },
        ]
      },
      {
        path: '/control',
        component: control,
        label: '飞控中心',  // 新增菜单项名称
        children:[
          {
            path: '/control/flysquare',
            component: Flysquare,
            label: '机场管理',  // 新增菜单项名称
          }, 
        ]
      },
      {
        path: '/task',
        component: task,
        label: '作业中心',  // 新增菜单项名称
        children:[
          {
            path: '/task/taskcontrol',
            component: Taskcontrol,
            label: '任务管理',  // 新增菜单项名称
          }, 
          {
            path: '/task/tasklog',
            component: Tasklog,
            label: '任务记录',  // 新增菜单项名称 
          },
          {
            path:'/task/taskline',
            component:Taskline,
            label:'航线规划'
          },
          {
            path:'/task/taskstop',
            component:Taskstop,
            label:'禁飞区管理'
          }
        ]
      },      
      {
        path: '/system',
        component: system,
        label: '系统中心',  // 新增菜单项名称
        children:[
          {
            path: '/system/person',
            component: Role,
            label: '人员管理',  // 新增菜单项名称
          }, 
        ]
      },
      {
        path: '/setting',
        component: User,
        label: '设置中心',
        show:false,
      }
    ]
  },
  {
    path:'/login',
    component:Login,
    label: '登录',  // 新增菜单项名称
  },
  {
    path:'/nopermission',
    component:Nopermission,
    label: '无权限',  // 新增菜单项名称 
  }
]

// 4. 生成路由对象
const router = createRouter({
  'history':createWebHashHistory(),  //路由模式
  'routes':routes   
})
// 路由守卫
router.beforeEach(async(to,from)=>{
  const account = useAccount()
//   // 路由白名单
  const whiteList = ['/login','/nopermission','/','/fly','/control','/task','/system']
  if(whiteList.includes(to.path)){
    console.log('路由白名单',to.path);
    return true;
  }
  // 登录状态判断
  let res = await checkToken(account.jwttoken)
  if(res.data.code==2){
    console.log('登录状态');
    // 访问权限控制 to.path是否存在permission中
    let isPermit = account.permitData.permission.includes(to.path)
    console.log('isPermit',isPermit);
    return isPermit ? true : '/nopermission'
    // // 修改开始：检查是否已存在当前路径
    // const exindex = account.pathList.indexOf(to.path);
    // if (exindex > -1) {
    //     account.pathList.splice(exindex, 1); // 移除已存在的路径
    // }

    // 数组中不存在该路径添加新路径
    // if(exindex==-1)account.pathList.push(to.path)
    // console.log('pathList',account.pathList);
  }else{
    window.$message.warning(res.data.msg)
   return '/login'
  }
})
// 5. 导出路由对象
export default router